// JavaScript Document
var saveObj,numObj,delObj,nameObj,upObj;
$(function(){
	saveObj=document.getElementById('save');
	numObj=getById("num");
	delObj=getById("del");
	nameObj=getById("name");
	upObj=getById("up");
	//初始化目录
	initTree();
	//绑定增加记录链接
	$(document.getElementById("add")).bind("click",function(){
		clearForm();
		var obj=getById("save");
		obj.attr('state','insert');
		obj.text('确定添加');
		numObj.removeAttr('disabled');
		delObj.css("display","none");
		
		return false;
	});
	//绑定添加/修改按钮链接
	$(saveObj).unbind("click");
	$(saveObj).bind("click",function(){
		var state=getById("save").attr('state');
		if(state=="insert"){
			insert();
		}else{
			update();
		}
		
		return false;
	});
	//绑定删除链接
	delObj.unbind('click');
	delObj.bind('click',function(){
		if(window.confirm(lang.del_before_msg)){
			del();
		}
		
		return false;
	});
});

function getAttr(obj){
	document.getElementById("num").value=obj.attr("num");
	document.getElementById("name").value=obj.text();
	document.getElementById("up").value=obj.attr("up");
}

function clearForm(){
	document.getElementById("num").value="";
	document.getElementById("name").value="";
	document.getElementById("up").value="";	
}

function initTree(){
	ajax({
		url:"index.php",
		data:{
			m:'Manage',
			a:'getCollege'	
		},
		callback:function(content){
			var json=text2json(content);
			if(json.state=="ok"){
				createTree(json.data);
				bindTreeEvent();
			}else{
				alert(ajax_tree_load_error);
			}
			
		}
	});	
}

function createTree(json){
	var html="<ul>";
	for(var i=0;i<json.length;i++){
		var num=json[i].tree_id;
		var name=json[i].tree_name;
		var up=json[i].tree_up;
		
		if(up==0){
			if(i==0){
				html+='<li><a href="javascript:void(0)" num="'+num+'" up="'+up+'">'+name+'</a><span>('+num+')</span><ul>';
			}else{
				html+='</ul><li><a href="javascript:void(0)" num="'+num+'" up="'+up+'">'+name+'</a><span>('+num+')</span><ul>';
			}
		}else{
			html+='<li><a href="javascript:void(0)" num="'+num+'" up="'+up+'">'+name+'</a><span>('+num+')</span></li>';	
		}
	}
	html+="</li></ul>";
	
	$('.left').html(html);
}

function bindTreeEvent(){
	$(".left a").bind("click",function(){
		getAttr($(this));
		var obj= getById('save');
		obj.attr('state','update');
		obj.text('确定保存');
		delObj.css("display","inline");
		
		numObj.attr('disabled','disabled');
	});	
}

function update(){
	if(!validata()) return false;
	ajax({
		url:"index.php",
		data:{
			m:"Manage",
			a:"update",
			id:numObj.val(),
			name:getById("name").val(),
			up:getById("up").val()
		},
		callback:function(content){
			var json=text2json(content);
			
			if(json.state=="ok"){
				alert(lang.ajax_ok);
				initTree();
			}else{
				alert(lang.ajax_add_error);	
			}
		}
	});
}

function insert(){
	if(!validata()) return false;
	ajax({
		url:"index.php",
		data:{
			m:"Manage",
			a:"add",
			id:numObj.val(),
			name:getById("name").val(),
			up:getById("up").val()
		},
		callback:function(content){
			var json=text2json(content);
			
			if(json.state=="ok"){
				alert(lang.ajax_ok);
				initTree();
			}else{
				alert(lang.ajax_add_error);	
			}
		}
	});	
}

function del(){
	if(!validata()) return false;
	ajax({
		url:"index.php",
		data:{
			m:"Manage",
			a:"del",
			id:numObj.val()
		},
		callback:function(content){
			var json=text2json(content);
			
			if(json.state=="ok"){
				alert(lang.ajax_ok);
				initTree();
				clearForm();
			}else{
				alert(lang.ajax_add_error);	
			}
		}
	});	
}

function validata(){
	var num=$.trim(numObj.val());
	var name=$.trim(nameObj.val());
	var up=$.trim(upObj.val());
	
	if(num==""){
		alert(lang.num_is_empty);
		return false;
	}else if(name==""){
		alert(lang.name_is_empty);
		return false;
	}else if(up==""){
		alert(lang.up_is_empty);	
		return false;
	}
	
	return true;
}